package com.forte.demo.robot.db.ban;

import com.forte.demo.robot.db.tools.DbUtil;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Timestamp;

import static com.forte.demo.robot.db.tools.GetTime.getNowString;
import static com.forte.demo.robot.db.tools.GetTime.getTime;


/**
 * @author SitaNya
 * 日期: 2019-08-03
 * 电子邮箱: sitanya@qq.com
 * 维护群(QQ): 162279609
 * 有任何问题欢迎咨询
 * 类说明:
 */
public class InputBanList {
    private static final Logger Log = LogManager.getLogger(InputBanList.class.getName());

    /**
     * 将QQ黑名单列表入库
     */
    public static void insertQqBanList(String qq, String botId, String reason) {
        Timestamp timestamp = getTime(getNowString());
        try (Connection conn = DbUtil.getConnection()) {
            //language=MySQL
            String sql = "INSERT INTO qqBanListBak(botId,qqId,reason,time) VALUES(?,?,?,?)";
            try (PreparedStatement ps = conn.prepareStatement(sql)) {
                ps.setString(1, botId);
                ps.setString(2, qq);
                ps.setString(3, reason);
                ps.setTimestamp(4, timestamp);
                ps.executeUpdate();
            }
        } catch (SQLException e) {
            Log.error(e.getMessage(), e);
        }
    }

    /**
     * 将QQ群黑名单列表入库
     */
    public static void insertGroupBanList(String groupId, String botId, String reason) {
        Timestamp timestamp = getTime(getNowString());
        try (Connection conn = DbUtil.getConnection()) {
            //language=MySQL
            String sql = "INSERT INTO groupBanListBak(botId,groupId,reason,time) VALUES(?,?,?,?)";
            try (PreparedStatement ps = conn.prepareStatement(sql)) {
                ps.setString(1, botId);
                ps.setString(2, groupId);
                ps.setString(3, reason);
                ps.setTimestamp(4, timestamp);
                ps.executeUpdate();
            }
        } catch (SQLException e) {
            Log.error(e.getMessage(), e);
        }
    }

    /**
     * 将QQ群黑名单列表入库
     */
    public static void deleteQqBanList(String qqId) {
        Timestamp timestamp = getTime(getNowString());
        try (Connection conn = DbUtil.getConnection()) {
            //language=MySQL
            String sql = "delete from qqBanListBak where qqId=? and botId=?";
            try (PreparedStatement ps = conn.prepareStatement(sql)) {
                ps.setString(1, qqId);
                ps.setString(2, "2694900224");
                ps.executeUpdate();
            }
        } catch (SQLException e) {
            Log.error(e.getMessage(), e);
        }
    }

    /**
     * 将QQ群黑名单列表入库
     */
    public static void deleteGroupBanList(String groupId) {
        Timestamp timestamp = getTime(getNowString());
        try (Connection conn = DbUtil.getConnection()) {
            //language=MySQL
            String sql = "delete from groupBanListBak where groupId=? and botId=?";
            try (PreparedStatement ps = conn.prepareStatement(sql)) {
                ps.setString(1, groupId);
                ps.setString(2, "2694900224");
                ps.executeUpdate();
            }
        } catch (SQLException e) {
            Log.error(e.getMessage(), e);
        }
    }

    /**
     * 将QQ群黑名单列表入库
     */
    public static void insertBanHisotry(String id, boolean isGroup) {
        try (Connection conn = DbUtil.getConnection()) {
            //language=MySQL
            String sql;
            if (isGroup) {
                sql = "INSERT INTO banHistoryBak(botId,Id,reason,inputTime,removeTime,isGroup) SELECT botId,groupId as Id,reason,time as inputTime,now() as removeTime,1 as isGroup from groupBanListBak where groupId=?";
            } else {
                sql = "INSERT INTO banHistoryBak(botId,Id,reason,inputTime,removeTime,isGroup) SELECT botId,qqId as Id,reason,time as inputTime,now() as removeTime,0 as isGroup from qqBanListBak where qqId=?";
            }
            try (PreparedStatement ps = conn.prepareStatement(sql)) {
                ps.setString(1, id);
                ps.executeUpdate();
            }
        } catch (SQLException e) {
            Log.error(e.getMessage(), e);
        }
    }
}
